﻿namespace Sidvall.Data
{
	public sealed class TransactionManager : Sidvall.Data.ITransactionScope
	{
		#region Business Members

		private System.Transactions.TransactionScope _TransactionScope;

		#region Complete

		public void Complete()
		{
			if (this._TransactionScope != null)
			{
				this._TransactionScope.Complete();
			}
		}

		#endregion
		#region Dispose

		public void Dispose()
		{
			if (this._TransactionScope != null)
			{
				this._TransactionScope.Dispose();
				this._TransactionScope = null;
			}
			System.GC.SuppressFinalize(this);
		}

		#endregion
		#region ToString

		public override string ToString()
		{
			return "TransactionManager - System.Transactions";
		}

		#endregion

		#endregion
		#region Factory Members

		#region GetTransactionScope

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
		public static TransactionManager GetTransactionScope(System.Transactions.TransactionScopeOption scopeOption, System.Transactions.IsolationLevel isolationLevel,
			System.TimeSpan? timeout, System.Transactions.TransactionScopeAsyncFlowOption asyncFlowOption)
		{
			TransactionManager item;
			System.Transactions.TransactionOptions transactionOptions;

			transactionOptions = new System.Transactions.TransactionOptions();
			transactionOptions.IsolationLevel = isolationLevel;
			if (timeout != null)
				transactionOptions.Timeout = timeout.Value;

			item = new TransactionManager();
			item._TransactionScope = new System.Transactions.TransactionScope(scopeOption, transactionOptions, asyncFlowOption);

			return item;
		}

		#endregion

		#endregion
	}
}
